// pages/mysubmit/mysubmit.js
Page({

  data: {
    c1:true,
    c2:false,
    c3:false,

    submitinfo:[]
  },
  //展示用户发布信息
  onLoad(){

    var that =this;
    //获取用户发布信息
    // 物品信息
    if(wx.getStorageSync('identity') == "学生用户"){
      wx.request({
        url: 'http://127.0.0.1:8080/item/showUserItem',
        method:"POST",
        data:{
          userid:wx.getStorageSync('openid')
        },
        success(res){
          if(res.data.length != 0){
            for(var i=0;i<res.data.length;i++){
              res.data[i].itemPic = JSON.parse(res.data[i].itemPic)
            }
          }
          var iteminfo = res.data;
          //用户发布的求租信息
          wx.request({
            url: 'http://127.0.0.1:8080/helpInfo/showHelpInfo',
            method:"POST",
            data:{
              userid:wx.getStorageSync('openid'),
            },
            success(res){
              for(var i=0;i<res.data.length;i++){
                res.data[i].helpPic = JSON.parse(res.data[i].helpPic)
              }
              //修改文件的键方便渲染
              res.data = JSON.parse(JSON.stringify(res.data).replace(/helpPic/g, "itemPic"))
              res.data = JSON.parse(JSON.stringify(res.data).replace(/helpTime/g, "submitDate"))
              res.data = JSON.parse(JSON.stringify(res.data).replace(/helpText/g, "itemTitle"))
              res.data = JSON.parse(JSON.stringify(res.data).replace(/helpInfoSee/g, "seeNum"))
              var helpinfo = res.data;
              var array = iteminfo.concat(helpinfo);
              // console.log(array)
              
              for(var i=0;i<array.length;i++){
                for(var j=0;j<array.length-i-1;j++){
                  var t1 = new Date(array[j].submitDate);
                  var t2 = new Date(array[j+1].submitDate);
                  if(t1 <t2){
                    var arr = array[j];
                    array[j] = array[j+1];
                    array[j+1] = arr;
                  }
                }
              }
  
              that.setData({
                submitinfo:array
              })
  
            }
          })
        }
      })
    }else{
      //房屋出租方信息
      wx.request({
        url: 'http://127.0.0.1:8080/rental/showUserRental',
        method:"POST",
        data:{userid:wx.getStorageSync('openid')},
        success(res){
          if(res.data.length != 0){
            for(var i=0;i<res.data.length;i++){
              res.data[i].rentalPic = JSON.parse(res.data[i].rentalPic)
            }
            // res.data = JSON.parse(JSON.stringify(res.data).replace(/monthRent/g, "dayRent"))
            res.data = JSON.parse(JSON.stringify(res.data).replace(/rentalPic/g, "itemPic"))
            res.data = JSON.parse(JSON.stringify(res.data).replace(/rentalTitle/g, "itemTitle"))
            res.data = JSON.parse(JSON.stringify(res.data).replace(/rentalSee/g, "seeNum"))
            res.data = JSON.parse(JSON.stringify(res.data).replace(/rentalState/g, "rentState"))
            
          that.setData({
            submitinfo:res.data
          })
          }
          
          // console.log(res)
        }
      })
    }
  
  },
  onShow(){
   if(this.data.c2){
    var that = this;
    wx.request({
      url: 'http://127.0.0.1:8080/item/showNoSubItem',
      method:"POST",
      data:{userid:wx.getStorageSync('openid')},
      success(res){
        // console.log(res)
        if(res.data.length != 0){
          for(var i=0;i<res.data.length;i++){
            res.data[i].itemPic = JSON.parse(res.data[i].itemPic)
          }
        }
        that.setData({
          submitinfo:res.data
        })
      }
   })
  }
  },

  c1(){
    this.onLoad();
    this.setData({
      c1:true,
      c2:false,
      c3:false
    })
  },
  c2(){
    if(wx.getStorageSync('identity') == "学生用户"){
      var that = this;
      wx.request({
        url: 'http://127.0.0.1:8080/item/showNoSubItem',
        method:"POST",
        data:{userid:wx.getStorageSync('openid')},
        success(res){
          // console.log(res)
          if(res.data.length != 0){
            for(var i=0;i<res.data.length;i++){
              res.data[i].itemPic = JSON.parse(res.data[i].itemPic)
            }
          }
          that.setData({
            submitinfo:res.data
          })
        }
      })
      this.setData({
        c1:false,
        c2:true,
        c3:false
      })
    }else{
      wx.showToast({
        title: '无法使用草稿信息',
        icon:"none"
      })
    }

  },
  c3(){
    var that = this;
    if(wx.getStorageSync('identity') == "学生用户"){
      wx.request({
        url: 'http://127.0.0.1:8080/item/haveDelItem',
        method:"POST",
        data:{
          userid:wx.getStorageSync('openid')
        },
        success(res){
          // console.log(res)
          if(res.data.length != 0){
            for(var i=0;i<res.data.length;i++){
              res.data[i].itemPic = JSON.parse(res.data[i].itemPic)
            }
          }
          that.setData({
            submitinfo:res.data
          })
        }
      })
    }else{
      wx.request({
        url: 'http://127.0.0.1:8080/rental/delHaveRental',
        method:"POST",
        data:{userid:wx.getStorageSync('openid')},
        success(res){
          if(res.data.length != 0){
            for(var i=0;i<res.data.length;i++){
              res.data[i].rentalPic = JSON.parse(res.data[i].rentalPic)
            }
            res.data = JSON.parse(JSON.stringify(res.data).replace(/rentalPic/g, "itemPic"))
            res.data = JSON.parse(JSON.stringify(res.data).replace(/rentalTitle/g, "itemTitle"))
            res.data = JSON.parse(JSON.stringify(res.data).replace(/rentalSee/g, "seeNum"))
            res.data = JSON.parse(JSON.stringify(res.data).replace(/rentalState/g, "state"))
          }
            that.setData({
              submitinfo:res.data
            })
          console.log(res)
        }
      })
    }
    
    this.setData({
      c1:false,
      c2:false,
      c3:true
    })
  },
  //查看发布效果
  checkfunction(e){
    var index = e.currentTarget.dataset.index;
    if(wx.getStorageSync('identity') == "学生用户"){
      var id = this.data.submitinfo[index].itemID;
      console.log(id)
      if(id != null){
        wx.navigateTo({
          url: '/pages/iteminfo/iteminfo?itemid='+id,
        })
      }else{
        var id = this.data.submitinfo[index].helpInfoID;
        wx.navigateTo({
          url: '/pages/helpdetail/helpdetail?helpinfoid='+id,
        })
        // console.log(id)
      }
    }else{
      var id = this.data.submitinfo[index].rentalID
      wx.navigateTo({
        url: '/pages/house/house?rentalid='+id,
      })
    }

  },

  /*删除发布 */
  del(e){
    var that = this;
    var index = e.currentTarget.dataset.index;
    if(wx.getStorageSync('identity') == "学生用户"){
      if(this.data.submitinfo[index].rentState == "租用中"){
        wx.showToast({
          title: '正在出租无法删除',
          duration: 1500,
          icon: "none",
          success: (res) => {},
        })
      }else{
        wx.showModal({
          title: '确定要删除吗',
          success: (result) => {
  
            if(result.confirm){
              wx.request({
                url: 'http://127.0.0.1:8080/item/delHaveItem',
                method:"POST",
                data:{
                  itemid:that.data.submitinfo[index].itemID
                },
                success(res){
                  if(res.data){
                    that.onLoad()
                  }
                  // console.log(res)
  
                }
              })
            }
  
          },
        })
      }
    }else{
      var id = that.data.submitinfo[index].rentalID
      // console.log(id)
      wx.request({
        url: 'http://127.0.0.1:8080/rental/changeRentalState',
        method:"POST",
        data:{
          rentalid:id
        },
        success(res){
          if(res.data){
            that.onLoad();
          }
        
          // console.log(res)
        }
      })
    }

    
  },
  /*修改发布 */
  change(e){
    var index = e.currentTarget.dataset.index
    var id = this.data.submitinfo[index].itemID;
    // console.log(this.data.submitinfo[index])
    if(id != null){
      wx.navigateTo({
        url: '/pages/itemsubmit/itemsubmit?itemid='+id+"&itemstate="+this.data.submitinfo[index].rentState,
      })
    }else{
      var id = this.data.submitinfo[index].helpInfoID;
      wx.navigateTo({
        url: '/pages/helpsubmit/helpsubmit?helpinfoid='+id,
      })
    }
  },
  //删除发布
  delsave(e){
    var that = this;
    wx.showModal({
      title: '确定删除该物品信息',
      complete: (res) => {
        if (res.confirm) {
          var index = e.currentTarget.dataset.index;
          var itemid = this.data.submitinfo[index].itemID
          wx.request({
            url: 'http://127.0.0.1:8080/item/delItem',
            method:"POST",
            data:{itemid:itemid},
            success(res){
              if(res.data){
                that.onShow();
                wx.showToast({
                  title: '删除成功',
                  icon:"success"
                })
              }
              console.log(res)
            }
          })
        }
      }
    })
   
  },
  // 重新上架物品
  renew(e){
    var that = this;
    
    var index = e.currentTarget.dataset.index;
    if(wx.getStorageSync('identity') == "学生用户"){
      var itemid = this.data.submitinfo[index].itemID;
      wx.request({
        url: 'http://127.0.0.1:8080/item/renewItem',
        method:"POST",
        data:{itemid:itemid},
        success(res){
          if(res.data){
            // that.onLoad();
            wx.showToast({
              title: '上架成功',
              duration: 1500,
              icon: "success",
              success: (res) => {
                wx.navigateBack({
                  delta: 1,
                  success: (res) => {},
                  fail: (res) => {},
                  complete: (res) => {},
                })
              },
            })
          }
          console.log(res)
        }
      })
    }else{
      var id = that.data.submitinfo[index].rentalID
      wx.request({
        url: 'http://127.0.0.1:8080/rental/rentalState',
        method:"POST",
        data:{
          rentalid:id
        },
        success(res){
          if(res.data){
            // that.onLoad();
            wx.navigateBack({
              delta: 1,
              success: (res) => {},
              fail: (res) => {},
              complete: (res) => {},
            })
          }
          // console.log(res)
        }      
      })
    }
   
  }
  

})